package com.zhiyou.app.shop.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PropertiesUtil {
	
	private static Log logger = LogFactory.getLog(PropertiesUtil.class);
	
	private static Properties sysConfigProperties;
	
	
	private static void initSysConfigProperties(){
		sysConfigProperties = PropertiesUtil.getProperties("/properties/config.properties");
	}
	
	/**
	 * 加载配置文件
	 * @param path
	 * @return
	 * @throws IOException 
	 */
	private static Properties getProperties(String path) {
		InputStream is = PropertiesUtil.class.getResourceAsStream(path);
		Properties props = new Properties();
		if(null != is) {
			try {
				props.load(is);
			} catch (IOException e) {
				logger.error("load properties on "+path+" failed", e);
				throw new RuntimeException(e);
			}finally {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return props;
	}
	
	public static String getSysProperties(String key){
		if(sysConfigProperties==null)
			initSysConfigProperties();
		return sysConfigProperties.getProperty(key);
	}
	
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		System.out.println(getSysProperties("upload.basepath"));
	}
}
